"""
自定义卡片组件
"""

from PyQt5.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout, QLabel, QFrame, QPushButton,
    QSizePolicy
)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QFont, QPixmap

class CardWidget(QFrame):
    """自定义卡片组件"""
    
    # 信号定义
    clicked = pyqtSignal()
    
    def __init__(self, title="", card_type="default", parent=None):
        super().__init__(parent)
        
        self.title = title
        self.card_type = card_type
        self.setup_ui()
        self.setup_style()
        
        # 设置大小策略
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        
        # 连接点击事件
        self.mousePressEvent = self.on_click
    
    def setup_ui(self):
        """设置用户界面"""
        layout = QVBoxLayout(self)
        layout.setContentsMargins(16, 16, 16, 16)
        layout.setSpacing(12)
        
        # 标题区域
        if self.title:
            title_layout = QHBoxLayout()
            
            self.title_label = QLabel(self.title)
            self.title_label.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
            title_layout.addWidget(self.title_label)
            
            title_layout.addStretch()
            
            # 更多按钮
            self.more_btn = QPushButton("...")
            self.more_btn.setFixedSize(24, 24)
            self.more_btn.setVisible(False)
            title_layout.addWidget(self.more_btn)
            
            layout.addLayout(title_layout)
            
            # 分隔线
            separator = QFrame()
            separator.setFrameShape(QFrame.HLine)
            separator.setStyleSheet("color: #e0e0e0;")
            layout.addWidget(separator)
        
        # 内容区域
        self.content_widget = QWidget()
        self.content_layout = QVBoxLayout(self.content_widget)
        self.content_layout.setContentsMargins(0, 0, 0, 0)
        self.content_layout.setSpacing(8)
        
        layout.addWidget(self.content_widget)
        
        # 底部区域（可选）
        self.footer_widget = QWidget()
        self.footer_layout = QHBoxLayout(self.footer_widget)
        self.footer_layout.setContentsMargins(0, 0, 0, 0)
        self.footer_layout.setSpacing(8)
        self.footer_widget.setVisible(False)
        
        layout.addWidget(self.footer_widget)
    
    def setup_style(self):
        """设置样式"""
        base_style = """
            CardWidget {
                background-color: #ffffff;
                border: 1px solid #e0e0e0;
                border-radius: 8px;
                margin: 4px;
            }
            CardWidget:hover {
                border-color: #1976d2;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            }
        """
        
        if self.card_type == "primary":
            style = base_style + """
                CardWidget {
                    border-left: 4px solid #1976d2;
                }
                QLabel {
                    color: #1976d2;
                }
            """
        elif self.card_type == "success":
            style = base_style + """
                CardWidget {
                    border-left: 4px solid #4caf50;
                }
                QLabel {
                    color: #4caf50;
                }
            """
        elif self.card_type == "warning":
            style = base_style + """
                CardWidget {
                    border-left: 4px solid #ff9800;
                }
                QLabel {
                    color: #ff9800;
                }
            """
        elif self.card_type == "danger":
            style = base_style + """
                CardWidget {
                    border-left: 4px solid #f44336;
                }
                QLabel {
                    color: #f44336;
                }
            """
        elif self.card_type == "info":
            style = base_style + """
                CardWidget {
                    border-left: 4px solid #2196f3;
                }
                QLabel {
                    color: #2196f3;
                }
            """
        else:
            style = base_style
        
        self.setStyleSheet(style)
    
    def add_content(self, widget):
        """添加内容组件"""
        self.content_layout.addWidget(widget)
    
    def add_content_layout(self, layout):
        """添加内容布局"""
        self.content_layout.addLayout(layout)
    
    def add_footer_button(self, text, callback=None):
        """添加底部按钮"""
        if not self.footer_widget.isVisible():
            self.footer_widget.setVisible(True)
        
        from .button import Button
        button = Button(text, button_type="secondary")
        if callback:
            button.clicked.connect(callback)
        
        self.footer_layout.addWidget(button)
        return button
    
    def set_title(self, title):
        """设置标题"""
        self.title = title
        if hasattr(self, 'title_label'):
            self.title_label.setText(title)
    
    def set_icon(self, icon_text):
        """设置图标"""
        if hasattr(self, 'title_label'):
            self.title_label.setText(f"{icon_text} {self.title}")
    
    def show_more_button(self, show=True):
        """显示/隐藏更多按钮"""
        if hasattr(self, 'more_btn'):
            self.more_btn.setVisible(show)
    
    def clear_content(self):
        """清空内容"""
        while self.content_layout.count():
            item = self.content_layout.takeAt(0)
            if item.widget():
                item.widget().deleteLater()
    
    def on_click(self, event):
        """点击事件"""
        self.clicked.emit()

class StatCardWidget(CardWidget):
    """统计卡片组件"""
    
    def __init__(self, title="", value="", icon="", color="primary", parent=None):
        super().__init__(title, color, parent)
        
        self.setup_stat_ui(value, icon)
    
    def setup_stat_ui(self, value, icon):
        """设置统计UI"""
        # 清空默认内容
        self.clear_content()
        
        # 统计内容布局
        stat_layout = QHBoxLayout()
        
        # 图标
        if icon:
            icon_label = QLabel(icon)
            icon_label.setFont(QFont("Microsoft YaHei", 24))
            icon_label.setStyleSheet(f"color: {self.get_color()};")
            stat_layout.addWidget(icon_label)
        
        # 数值
        self.value_label = QLabel(value)
        self.value_label.setFont(QFont("Microsoft YaHei", 20, QFont.Bold))
        self.value_label.setStyleSheet(f"color: {self.get_color()};")
        stat_layout.addWidget(self.value_label)
        
        stat_layout.addStretch()
        
        self.add_content_layout(stat_layout)
    
    def get_color(self):
        """获取颜色"""
        color_map = {
            "primary": "#1976d2",
            "success": "#4caf50",
            "warning": "#ff9800",
            "danger": "#f44336",
            "info": "#2196f3"
        }
        return color_map.get(self.card_type, "#1976d2")
    
    def set_value(self, value):
        """设置数值"""
        if hasattr(self, 'value_label'):
            self.value_label.setText(str(value))